"use strict";
/* Controllers */
app.controller("productInfoEditController", ['$scope', '$location', '$rootScope', '$q', '$routeParams', 'productInfoEditService', function ($scope, $location, $rootScope, $q, $routeParams, productInfoEditService) {

    var vm = $scope.vm = {};
    vm.p = {};
    var routeId = $routeParams.id;


    function getNewObjectOf(src) {
        return Object.assign({}, src);
    }

    //e_time\s*[\)]?\s*[>|>=|<|<=|=]{1,2}\s*['|"]{1}%s['|"]{1}
    var errorFn = function (result) {
        alert(result);
    };

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    //    $scope.validateSql=function(){
    //        console.log("sql"+vm.p.sql)
    //    }

    $scope.getProductInfo = function () {
        var vm = $scope.vm;
        var params = {
            id: routeId
        };
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data
            } else {
                alert(res.msg);
            }
        };
        return productInfoEditService.getProductInfo(params, procFn, errorFn);
    };

    $scope.productInfoSave = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("添加成功！")
            } else {
                alert(response.msg);
            }
        };
        productInfoEditService.productInfoSave(params, procFn, errorFn);
    };

    if (typeof (routeId) != "undefined" && routeId != null) {
        $scope.getProductInfo()
    }


}]);


/* Services */
app.service('productInfoEditService', ['ajaxService', function (ajaxService) {

    this.productInfoSave = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/produce_info/add.do", successFunction, errorFunction);
    };

    this.getProductInfo = function (params, successFunction, errorFunction) {
        console.log("params is -----")
        console.log(params)
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/produce_info/detail.do", successFunction, errorFunction);
    };

}]);

